1. /*factN2pf.cpp by K.Tsuru */
  2. // since ver 2.181
  3. #ifndef TOOLS_H
  4. #include "tools.h"
  5. #endif // TOOLS_H
  6. /***************************************************
  7. It evaluates
  8. N N N N
  9. p= - + --- + --- +...+ ---
  10. m m^2 m^3 m^k
  11. This is the exponent of m factorizing N into prime factor as
  12. N = ...(m^p)...
  13. long(32 bits) version causes overflow error in 2nd term
  14. for example
  15. m = 128461
  16. m^2 = 16502228521 > LONG_MAX = 2147483647
  17. Then it needs (*) statement.
  18. ****************************************************/
  19. static const int SQRT_INT_MAX = (int)floor(sqrt((double)INT_MAX)); // = 46340 = sqrt(2147483647) its square is 2147395600
  20. static int powerOf(const int N, const int m) {
  21. if(m > SQRT_INT_MAX) return (N/m); // (*)
  22. int d = m, p = 0;
  23. while(N >= d) {
  24. p += N/d; d *= m;
  25. }
  26. return p;
  27. }
  28. /*********************************
  29. N! factorization into prime factor
  30. N! = a^p b^q c^r ...x y z
  31. Returns the size of pf[].
  32. **********************************/
  33. int FactorialIntoPrimeFactor(ulong N, SNBlock <primeFactor>& pf) {
  34. NCBlock <ulong> P;
  35. int ts = MakePrimeTable(P, N); // table size
  36. pf.size( ts, 0); // allocate memory
  37. int c;
  38. for(c = 0; P(c) > 0; c++) {
  39. ulong t = P(c);
  40. pf[c].prime = t;
  41. pf[c].power = powerOf(N, t);
  42. }
  43. pf.reserve(c);
  44. pf[c].prime = 1;
  45. pf[c].power = 0;
  46. return c;
  47. }

factN2pf.cpp : last modifiled at 2016/11/09 08:46:32(1,370 bytes)
created at 2016/04/11 11:17:20
The creation time of this html file is 2017/10/07 10:54:15 (Sat Oct 07 10:54:15 2017).